﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using docNET.Resources;
using docNET.Common;

namespace docNET
{
	/// <summary>
	/// Options windows form.
	/// </summary>
	public partial class OptionsForm : Form
	{
		#region C'tor
		/// <summary>
		/// Initializes a new instance of the options windows form.
		/// </summary>
		public OptionsForm()
		{
			InitializeComponent();

			// Get values from settings.
			var settings = docNET.Properties.Settings.Default;
			defaultFolderTextBox.Text = defaultFolderTextBox.OriginalValue = settings.DefaultProjectPath;
			showExceptionsCheckBox.Checked = showExceptionsCheckBox.OriginalValue = settings.ShowExceptions;
		}
		#endregion

		#region Properties
		/// <summary>
		/// Checks if any settings has been changed and need to be saved.
		/// </summary>
		private bool HasChanges
		{
			get
			{
				return defaultFolderTextBox.IsChanged || showExceptionsCheckBox.IsChanged;
			}
		}
		#endregion

		#region Methods
		/// <summary>
		/// Saves any changed settings and refreshes Current.
		/// </summary>
		/// <see cref="docNET.Current"/>
		private void SaveSettings()
		{
			var settings = docNET.Properties.Settings.Default;
			if (defaultFolderTextBox.IsChanged)
				settings.DefaultProjectPath = defaultFolderTextBox.Text;
			if (showExceptionsCheckBox.IsChanged)
				settings.ShowExceptions = showExceptionsCheckBox.Checked;
			settings.Save();
			Current.Refresh();
		}
		#endregion

		#region Form Events
		private void defaultFolderBrowseButtonClick(object sender, EventArgs e)
		{
		}

		private void cancelButtonClick(object sender, EventArgs e)
		{
			if (HasChanges)
			{
				switch (MessageBox.Show(Messages.QueryWantToSaveSettings, Helper.GetAppName(), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
				{
					case DialogResult.Yes:
						SaveSettings();
						break;
					case DialogResult.Cancel:
						return;
				}
			}

			Close();
		}

		private void okButtonClick(object sender, EventArgs e)
		{
			if (HasChanges)
				SaveSettings();

			Close();
		}
		#endregion
	}
}
